import 'package:ball_lion/module/pick/items/avator_label.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/expert/plan_pack/plan_pack_history_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/widget/history_label.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';

import 'package:ball_lion/entity/expert/plan_pack/plan_pack_entity.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/widgets/card.dart';

class PlanPackHistoryPage extends StatefulWidget {
  const PlanPackHistoryPage({super.key});

  @override
  State<PlanPackHistoryPage> createState() => _PlanPackHistoryPageState();
}

class _PlanPackHistoryPageState extends State<PlanPackHistoryPage> {
  final PlanPackHistoryLogic logic =
      Get.put(PlanPackHistoryLogic(), tag: "${Get.arguments}");

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlanPackHistoryLogic>(
        tag: "${Get.arguments}",
        builder: (logic) {
          return Scaffold(
            backgroundColor: Colours.greyF2F3F7,
            appBar: Styles.appBar(
                title: QsText("${logic.expertId.hasValue ? "专家" : ""}历史套餐")),
            body: EasyRefresh.builder(
                controller: logic.refreshController,
                onRefresh: logic.getRefresh,
                onLoad: logic.loadMore,
                footer:
                    Styles.refreshFooter(position: IndicatorPosition.locator),
                childBuilder: (BuildContext context, ScrollPhysics physics) =>
                    CustomScrollView(
                      physics: physics,
                      slivers: [
                        LoadingWidget<bool>.sliver(
                          physics: physics,
                          isLoading: logic.data == null,
                          data: logic.data?.length == 0,
                          child: () => SliverList.builder(
                              itemCount: logic.data?.length ?? 0,
                              itemBuilder: (context, index) =>
                                  item(logic.data![index])),
                        ),
                        const FooterLocator.sliver()
                      ],
                    )),
          );
        });
  }

  Widget item(PlanPackEntity entity) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () =>
          Get.toNamed(Routes.planPackDetail, arguments: entity.packageId),
      child: Block(
          cardStyle: const CardStyle(
              padding: EdgeInsets.all(12),
              margin: EdgeInsets.fromLTRB(8, 8, 8, 0)),
          child: Stack(
            alignment: AlignmentDirectional.topEnd,
            children: [
              Column(children: [
                AvatarLabel.record(
                    avatarUrl: entity.expertLogo,
                    name: entity.expertName,
                    id: entity.expertId),
                Container(height: 8),
                QsText(entity.title,
                    maxLines: 2, color: Colours.grey66, fontSize: 14),
              ]),
              if (entity.packageTag.hasValue)
                HistoryLabel.planPack(text: entity.packageTag ?? "")
                    .paddingOnly(top: 4, right: 4)
            ],
          )),
    );
  }
}
